package com.frame.work.frame.deal.device0fbf;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.frame.work.common.constant.TypeEnum;
import com.frame.work.common.util.JsonToMqtt;
import com.frame.work.common.util.YmlStatic;
import com.frame.work.config.SpringUtil;
import com.frame.work.entity.DeviceAssociatedInformation;
import com.frame.work.entity.TblFingerprint;
import com.frame.work.frame.deal.base.BaseFrame;
import com.frame.work.frame.deal.util.DeviceRelationMessageUtil;
import com.frame.work.frame.deal.util.WeakCurrentDeviceUtil;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 指纹锁德施曼
 */
@Data

public class ZhiWenSuo_DeShiMan_0FBF0202 extends BaseFrame {

    private String switchState;

    public ZhiWenSuo_DeShiMan_0FBF0202(String gatewayMac, String data) {

        //将基础数据进行处理
        super(gatewayMac, data);
        this.setSwitchState(getSwitchState(data));
        //获取基础数据
        List<DeviceAssociatedInformation> deviceUserMessage = DeviceRelationMessageUtil.getDeviceUserMessage(this.getDeviceMac());
        if (CollectionUtils.isEmpty(deviceUserMessage)) {
            getAction(data);
            //将数据发送到mqtt队列中
            JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));
            deviceUserMessage = DeviceRelationMessageUtil.getDeviceUserMessage(this.getDeviceMac());
            try {
                WeakCurrentDeviceUtil.openTheDoorByKey(deviceUserMessage.get(0), getKeyId(data), getUserTypeToName(data), getUserType(data));
            } catch (Exception e) {
                System.out.println("e = " + e.getMessage());
            }

        }

        switch (this.getDataType()) {
            case "00":
                String type = TypeEnum.STATE.code();
                String content = "";
                //钥匙类型
                switch (getKeyType(data)) {
                    //指纹
                    case "01":
                        TblFingerprint fingerprint = WeakCurrentDeviceUtil.openTheDoorByKey(deviceUserMessage.get(0), getKeyId(data), getUserTypeToName(data), getUserType(data));
                        content = fingerprint.getName();
                        // 场景触发
                        WeakCurrentDeviceUtil.sceneTrigger(fingerprint);
                        break;
                    //密码 (本地,还是时效用户，还是一次性用户)
                    case "02":
                        content = "使用";
                        if (StringUtils.equals("01", getUserType(data))) {
                            content += "本地密码";
                        } else if (StringUtils.equals("04", getUserType(data))) {
                            content += "临时密码";
                            RabbitTemplate rabbitTemplate = SpringUtil.getBean(RabbitTemplate.class);
                            Map<String, Object> map = new HashMap<>(3);
                            //删除一次性密码数据
                            map.put("dealType", "deleteSqlOncePsd");
                            map.put("type", "once");
                            map.put("deviceMac", this.getDeviceMac());
                            rabbitTemplate.convertAndSend(YmlStatic.getGeneralQueueProcessing(), map);
                        } else if (StringUtils.equals("05", getUserType(data))) {
                            content += "访客密码";
                        }
                        break;
                    //遥控(远程开门)
                    case "03":
                        content = "遥控";
                        break;
                    //卡
                    case "04":
                        content = "使用卡";
                        break;
                    //机械钥匙
                    case "05":
                        content = "钥匙";
                        break;
                    //App远程
                    case "06":
                        content = "远程";
                        break;
                    //语音
                    case "07":
                        content = "语音";
                        break;
                    //人脸
                    case "08":
                        content = "人脸";
                        break;
                    //虹膜
                    case "09":
                        content = "虹膜";
                        break;
                    //静脉
                    case "0A":
                        content = "静脉";
                        break;
                    //离家
                    case "0B":
                        content = "离家";
                        break;
                    //错误情况
                    default:
                        //提示标志不是00说明是提示上报，如果为00说明是故障上报
                        if (StringUtils.equals("003FFFFF", getChildOdIndex(data))) {
                            content = !StringUtils.equals("00", getAlarm(data)) && StringUtils.equals("00", getError(data)) ? getAlarmName(data) : getErrorName(data);
                            type = StringUtils.equals("00", getAlarm(data)) && !StringUtils.equals("00", getError(data)) ? TypeEnum.WARNING.code() : TypeEnum.NORMAL.code();
                        }
                        break;
                }
                content = content + (StringUtils.equals("01", this.switchState) ? "开锁" : "");
                WeakCurrentDeviceUtil.SaveLogAndSendJpush(this.getDeviceMac(), this.getGatewayMac(), content, type);
                break;
            case "80":
                break;
            default:
                break;
        }

        getAction(data);
        //将数据发送到mqtt队列中
        JsonToMqtt.sendMQTT(gatewayMac, JSONObject.toJSONString(this));

    }
    //****************************************************指纹锁静态方法**************************************************************

    /**
     * @Description:将actions信息放入json对象中
     * @param: data
     * @return: java.util.Map<java.lang.String, java.lang.Object>
     * @author:wjming
     * @Date:2021/1/26 18:38
     */
    private Map<String, Object> getAction(String data) {
        Map<String, Object> actions = new HashMap();
        actions.put("switchState", switchState);
        actions.put("dianya", getDianYa(data));
        this.setActions(actions);
        return actions;
    }

    private String getSwitchState(String data) {
        return StringUtils.substring(data, 58, 60);
    }

    /**
     * @Description:截取指纹编号
     * @author:wjming
     * @Date:2021/1/26 18:48
     */
    private static String getKeyId(String data) {
        return StringUtils.substring(data, 66, 70);
    }

    /**
     * @Description:用户类型
     * @author:wjming
     * @Date:2021/1/26 19:43
     */
    private static String getUserType(String data) {
        return StringUtils.substring(data, 62, 64);
    }

    /**
     * @Description:获取报警编码信息
     * @author:wjming
     * @Date:2021/1/26 19:30
     */
    private static String getAlarm(String data) {
        return StringUtils.substring(data, data.length() - 14, data.length() - 12);
    }

    /**
     * @Description:获取故障编码
     * @author:wjming
     * @Date:2021/1/26 19:31
     */
    private static String getError(String data) {
        return StringUtils.substring(data, data.length() - 10, data.length() - 8);
    }

    /**
     * @Description:获取钥匙类型
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/28 14:36
     */
    private static String getKeyType(String data) {
        return StringUtils.substring(data, 64, 66);
    }


    /**
     * @Description:获取电压
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/27 10:40
     */
    private static String getDianYa(String data) {
        String dianya = StringUtils.substring(data, 50, 54);
        dianya = Integer.parseInt(dianya.substring(0, 2), 16) + "";
        return dianya;
    }

    /**
     * @Description:获取子索引选项
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/2/2 19:40
     */
    private static String getChildOdIndex(String data) {
        return StringUtils.substring(data, 26, 34);
    }

    /**
     * @Description:报警编码对应的报警类型
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/26 19:32
     */
    private static String getAlarmName(String data) {
        String alarmName = "触发";
        String alarm = getAlarm(data);
        switch (alarm) {
            case "01":
                alarmName = alarmName + "防撬警报";
                break;
            case "02":
                alarmName = alarmName + "非法操作";
                break;
            case "03":
                alarmName = alarmName + "低电量警报";
                break;
            case "04":
                alarmName = alarmName + "劫持警报";
                break;
            case "05":
                alarmName = alarmName + "门未锁好警报";
                break;
            case "06":
                alarmName = alarmName + "密码错误多次锁定";
                break;
            case "07":
                alarmName = alarmName + "指纹错误多次锁定";
                break;
            case "08":
                alarmName = alarmName + "门卡错误多次锁定";
                break;
            case "09":
                alarmName = alarmName + "钱包多次锁定";
                break;
            default:
                break;
        }
        System.out.println("警报类型：" + alarmName);
        return alarmName;
    }

    /**
     * @Description:故障类型
     * @param: data
     * @return: java.lang.String
     * @author:wjming
     * @Date:2021/1/26 19:40
     */
    private static String getErrorName(String data) {
        String errorName = "";
        String errorCode = getError(data);
        if (StringUtils.equals("00", errorCode)) {
            errorName = "无故障";
        } else if ("01".equals(errorCode)) {
            errorName = "欠压故障(" + getDianYa(data) + "%)";
        } else if ("02".equals(errorCode)) {
            errorName = "指纹故障";
        } else if ("03".equals(errorCode)) {
            errorName = "触摸屏故障";
        } else if ("04".equals(errorCode)) {
            errorName = "时钟故障";
        }
        return errorName;
    }

    private static String getUserTypeToName(String data) {
        String userType = getUserType(data);
        String userName = "";
        if ("01".equals(userType)) {
            userName = "管理员";
        } else if ("02".equals(userType)) {
            userName = "普通用户";
        } else if ("03".equals(userType)) {
            userName = "劫持用户";
        } else if ("04".equals(userType)) {
            userName = "临时用户";
        } else if ("05".equals(userType)) {
            userName = "时效用户";
        } else if ("06".equals(userType)) {
            userName = "一次性用户";
        }
        return userName;
    }
}
